<?php
/**
 * 用户
 *
 * @author Administrator
 *
 */
namespace App;

use Illuminate\Support\Facades\URL;

use \DB;
use Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    public $timestamps = false;

    protected $table = 'admin';

    protected $primaryKey = 'id';

    public function role()
    {
        return $this->hasOne('App\Role', 'current_role', 'id');
    }

    /**
     * 获得用户top-nav权限列表
     */
    static function getPermissionNav($cache = 0)
    {
        $submodules = \Cache::has('submodules') ? \Cache::get('submodules') : false;

        if ($submodules === false || $cache) {
            $data = array();
            $user = Auth::user();
            $user_role = DB::table('adm_role_user as aru')->join("adm_role as ar", "aru.role_id", "=", "ar.id")->join('admin', 'aru.admin_id', '=', 'admin.id')->select("ar.*")->where(
                'aru.admin_id',
                $user->id
            )->first();
            if ($user_role->type == 'System') {
                $submodules = DB::table('adm_submodule')->where('status', 1)->orderBy('sort', 'asc')->get();
            } else {
                $submodules = DB::table('adm_role_submodule as ars')
                    ->join('adm_submodule as as', 'ars.submodule_id', '=', 'as.id')
                    ->select('as.*')
                    ->where('ars.role_id', $user_role->id)->where('as.status', 1)
                    ->orderBy('as.sort', 'asc')
                    ->get();
            }
            foreach ($submodules as $submodule) {
                $data[$submodule->id]['id'] = $submodule->id;
                $data[$submodule->id]['name'] = $submodule->name;
                $data[$submodule->id]['code'] = $submodule->code;
                $data[$submodule->id]['parent_id'] = $submodule->pid;
                $data[$submodule->id]['url'] = action($submodule->code . '@getIndex', ['submodule_id' => $submodule->id]);
            }
            $data = toTree($data, 'id');
            $submodules = $data;
            \Cache::add('submodules', $data, 86400);
        }

        return $submodules;
// 		return ['nav'=>[1=>'权限系统']];
    }

    /**
     * 获得用户左侧权限列表
     */
    static function getPermissionMenu($submodule_id, $cache = 0)
    {
        $controllers = \Cache::has('controllers') ? \Cache::get('controllers') : false;

        if ($controllers === false || $cache) {
            $data = array();
            $user = Auth::user();
            $user_role = DB::table('adm_role_user as aru')->join("adm_role as ar", "aru.role_id", "=", "ar.id")->join('admin', 'aru.admin_id', '=', 'admin.id')->select("ar.*")->where(
                'aru.admin_id',
                $user->id
            )->first();
            if ($user_role->type == 'System') {
                $controllers = DB::table('adm_controller')->where('submodule_id', $submodule_id)->where('status', 1)->orderBy('sort', 'asc')->get();
            } else {
                $controllers = DB::table('adm_role_controller as arc')
                    ->join('adm_controller as ac', 'arc.controller_id', '=', 'ac.id')
                    ->select('ac.*')
                    ->where('arc.role_id', $user_role->id)->where('ac.submodule_id', $submodule_id)->where('ac.status', 1)
                    ->orderBy('ac.sort', 'asc')
                    ->get();
            }
            foreach ($controllers as $controller) {
                $data[$controller->id]['id'] = $controller->id;
                $data[$controller->id]['name'] = $controller->name;
                $data[$controller->id]['code'] = $controller->code;
                $data[$controller->id]['parent_id'] = $controller->pid;
                $data[$controller->id]['url'] = !empty($controller->code) ? action($controller->code, ['controller_id' => $controller->id]) : '';
// 				$actions = self::getPermissionAction($controller->id,1);
// 				$data[$controller->id]['actions'] = !empty($actions[$controller->id]) ? $actions[$controller->id] : array();
            }
            $data = toTree($data, 'id');
            $controllers = $data;
            \Cache::add('controllers', $data, 86400);
        }

        return $controllers;
// 		return ['menu'=>[1=>'用户管理']];
    }

    static function getPermissionAction($controller_id, $cache = 0)
    {
        $actions = \Cache::has('actions') ? \Cache::get('actions') : false;

        if ($actions === false || $cache) {
            $data = array();
            $user = Auth::user();
            $user_role = DB::table('adm_role_user as aru')->join("adm_role as ar", "aru.role_id", "=", "ar.id")->join('admin', 'aru.admin_id', '=', 'admin.id')->select("ar.*")->where(
                'aru.admin_id',
                $user->id
            )->first();
            if ($user_role->type == 'System') {
                $actions = DB::table('adm_action')->where('status', 1)->where('controller_id', $controller_id)->orderBy('sort', 'asc')->get();
            } else {
                $actions = DB::table('adm_role_action as ara')
                    ->join('adm_action as aa', 'ara.action_id', '=', 'aa.id')
                    ->select('aa.*')
                    ->where('ara.role_id', $user_role->id)->where('aa.controller_id', $controller_id)->where('aa.status', 1)
                    ->orderBy('aa.sort', 'asc')
                    ->get();
            }
            foreach ($actions as $action) {
                $data[] = $action->code;
            }
            $actions = $data;
            \Cache::add('actions', $data, 86400);
        }

        return $actions;
    }

    static function loginSuccess($user)
    {
        Auth::loginUsingId($user->id);
        $user->last_ip = getIP();
        $user->login_count += 1;
        $user->save();
        $user_role = DB::table('adm_role_user as aru')->join("adm_role as ar", "aru.role_id", "=", "ar.id")->join('admin', 'aru.admin_id', '=', 'admin.id')->select("ar.*")->where(
            'aru.admin_id',
            $user->id
        )->first();
        session(['user_role' => $user_role]);
    }
}
